// pages/component/ShareBrokerageItem/ShareBrokerageItem.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { receiveAward } from '../../../api/reward';
import { getProjectRewardContractUrl } from '../../../api/home';

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      value: {
        amount: 0,
      }
    }
  },

  options: {
    addGlobalClass: true,
  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    showPopup: false,
    dialogType: 1,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 领取佣金
    receive(e){
      let awardId = e.target.dataset.id||e.currentTarget.dataset.id;
      let _this = this;
      receiveAward(awardId)
      .then(res => {
        if(res.success){
          // _this.triggerEvent('itemevent', {},)
          wx.navigateTo({
            url: '/pages/Projects/ReceiveAward/ReceiveAward?awardId=' + awardId,
          })
        }else{
          if(res.code == 'NOT_SIGN_AWARD_CONTRACT'){
            _this.setData({
              showPopup: true,
              dialogType: 1,
            })
          }else if(res.code == 'NO_AWARD_CONTRACT'){
            _this.setData({
              showPopup: true,
              dialogType: 2,
            })
          }else{
            wx.showToast({
              title: res.errMessage,
              icon: 'none',
            })
          }
        }
      })
      
    },
    // 关闭弹框
    closePop(){
      this.setData({
        showPopup: false,
      })
    },
    // 跳转详情
    goDetail(){
      wx.navigateTo({
        url: '/pages/Projects/ShareBrokerageDetail/ShareBrokerageDetail?rewardId=' + this.item.id,
      })
    },
    // 获取签约链接
    getContractUrl(){
      let _this = this;
      getProjectRewardContractUrl(_this.item.companyId)
      .then(res => {
        if(res.success){
          wx.navigateTo({
            url: '/pages/link/link/link?url=' + encodeURIComponent(res.data) + '&title=' + JSON.stringify(name),
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
  }
})
